﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Activities;

namespace Lisphi.Lab.ProfWFChap05
{
    public class EnrollmentService : IEnrollmentService
    {
        public void RequestEnrollmentStatus(string name)
        {
            ThreadPool.QueueUserWorkItem(GetResponse,
                new EnrollmentRequestEventArgs(
                    WorkflowEnvironment.WorkflowInstanceId,
                    name));
        }

        private void GetResponse(object o)
        {
            EnrollmentRequestEventArgs args = o as EnrollmentRequestEventArgs;

            Console.WriteLine("Will you approve the application for " +
                args.ApplicantName + "?");

            // read the user's response from the command line
            char response = Console.ReadKey().KeyChar;
            Console.WriteLine();

            // check the user's response
            // and raise the appropriate event
            if (response == 'y')
            {
                EnrollmentApproval(null, new EnrollmentApprovalEventArgs(args.InstanceId, true));
            }
            else
            {
                EnrollmentApproval(null, new EnrollmentApprovalEventArgs(args.InstanceId, false));
            }

        }

        public event EventHandler<ExternalDataEventArgs> EnrollmentApproval;
    }


}
